﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace yule.DAL.Sys
{
    /// <summary>
    /// 地区数据访问类
    /// </summary>
    public partial class Region
    {
        /// <summary>
        /// 更新系统地区(Sys_Region)
        /// </summary>
        /// <param name="model"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        public int UpdateX(yule.Model.Sys.Region model, yule.Data.Transaction tran)
        {
            int val = 0;

            val = this.Update(model, tran);

            IList<yule.Model.Sys.Region> childs = this.GetAllChilds(model, tran);

            yule.Model.Sys.Region oldModel = this.Select(model.Id);

            int level = oldModel.PathId.Split(yule.Model.TreeHelper.TreeSeparator).Length - 1;

            if (childs != null && childs.Count > 0)
            {
                foreach (yule.Model.Sys.Region child in childs)
                {
                    string[] pathids = child.PathId.Split(yule.Model.TreeHelper.TreeSeparator);
                    string[] pathnames = child.PathName.Split(yule.Model.TreeHelper.TreeSeparator);

                    string pathid = model.PathId;
                    string pathname = model.PathName;

                    for (int i = (level + 1); i < pathids.Length; i++)
                    {
                        string t_pathid = pathids[i];
                        string t_pathname = pathnames[i];
                        if (yule.Util.Verificate.IsNull(t_pathid) == false)
                        {
                            pathid += yule.Model.TreeHelper.TreeSeparator + t_pathid;
                            pathname += yule.Model.TreeHelper.TreeSeparator + t_pathname;
                        }
                    }

                    child.PathId = pathid;
                    child.PathName = pathname;

                    this.Update(child, tran);
                }
            }

            return val;
        }

        private IList<Model.Sys.Region> GetAllChilds(yule.Model.Sys.Region model, yule.Data.IExecuteQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append("select * from Sys_Region t where t.PathId like '" + model.PathId + yule.Model.TreeHelper.TreeSeparator + "%'");

            System.Data.DataTable dt = dbHelper.ExecuteDataTable(sql.ToString());

            IList<yule.Model.Sys.Region> list = this.ConverToList<yule.Model.Sys.Region>(dt,
                (m) => { this.SetCache<yule.Model.Sys.Region>(m); });

            return list;
        }


        public IList<Model.Sys.Region> GetChilds(yule.Model.Sys.Region model, yule.Data.IExecuteQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append("select * from Sys_Region where ParentId = '" + model.Id + "'");

            System.Data.DataTable dt = dbHelper.ExecuteDataTable(sql.ToString());

            IList<yule.Model.Sys.Region> list = this.ConverToList<yule.Model.Sys.Region>(dt,
                (m) => { this.SetCache<yule.Model.Sys.Region>(m); });

            return list;
        }

        public IList<Model.Sys.Region> GetChilds(yule.Model.Sys.Region model)
        {
            return this.GetChilds(model, yule.Data.DBHelper.Instance);
        }

    }
}